#!/usr/bin/env python
# coding=utf-8

import os
import sys
import time
import anydbm
import zlib
import glob


def main():
	if len(sys.argv) == 1:
		return
	else:
		dbfolder = sys.argv[1]
	os.chdir(dbfolder)
	dbns = (s[:-4] for s in glob.glob(r'*.dat'))
	for dbn in dbns:
			extract_db(dbn)

def extract_db(dbn):
	db = anydbm.open(dbn, 'w')
	for key in db.iterkeys():
		try:
			fn = key + '.bmp'
			with open(fn, 'wb') as f:
				f.write(zlib.decompress(db[key]))
		except:
			pass
	db.close()

if __name__ == '__main__':
	main()

